package bean;

import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.data.extensions.Email;
import com.google.gdata.data.extensions.Name;
import entity.Contact;
import entity.RegisteredUser;
import java.io.Serializable;
import java.net.URL;
import manager.ContactManager;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author dzaric
 */
public class GmailImportBean implements Serializable{

    protected String token;
    protected boolean started;
    protected boolean finished;

    public GmailImportBean() {
        
        started = false;
        finished = false;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public boolean isStarted() {
        System.out.println("is started" + started);
        return started;
    }

    public void setStarted(boolean started) {
        this.started = started;
    }

    public boolean isFinished() {
        return finished;
    }

    public void setFinished(boolean finished) {
        this.finished = finished;
    }

    public void setImportContacts(RegisteredUser user) {
        if (this.started) {
            return;
        }
        try {
            ContactManager cm = new ContactManager();

            this.started = true;
            String sessionToken = this.token;
            ContactsService myService = new ContactsService("Moja aplikacija");
            myService.setAuthSubToken(sessionToken);
            // Request the feed
            URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full?max-results=10000");
            ContactFeed resultFeed = myService.getFeed(feedUrl, ContactFeed.class);
            // Print the results

            System.out.println(resultFeed.getTitle().getPlainText());
            for (int i = 0; i < resultFeed.getEntries().size(); i++) {

                Contact newContact = new Contact();
                newContact.setRegisteredUser(user);

                ContactEntry entry = resultFeed.getEntries().get(i);
                System.out.print("Name: ");
                if (entry.hasName()) {
                    Name name = entry.getName();
                    String[] names = name.getFullName().getValue().split(" ");
                    newContact.setFirstName(names[0]);
                    if (names.length > 1) {
                        newContact.setLastName(names[1]);
                    } else {
                        newContact.setLastName("N/A");
                    }
                    System.out.println(name.getFullName().getValue());
                } else {
                    newContact.setFirstName("N/A");
                    newContact.setLastName("N/A");
                    System.out.println("N/A");
                }
                System.out.print("\t");

                String new_email = null;
                for (Email email : entry.getEmailAddresses()) {
                    new_email = email.getAddress();
                    System.out.print(" " + email.getAddress());
                    break;
                }
                if (new_email != null && cm.contactExists(user, new_email) == false) {
                    newContact.setEmail(new_email);
                    newContact.getRegisteredUser().getContacts().add(newContact);
                    cm.create(newContact);
                }

                System.out.println("\n");
            }
            this.finished = true;
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
